/*
 * @Author: chengjiang
 * @Date: 2025-04-25 14:23:12
 * @Description:
 */

import { reactive } from 'vue';

import { defineStore } from 'pinia';

/**
 * antd使用 select和radio通用
 * 'boba' | 'in' | 'out' | 'price' | 'sale'
 */
export interface OrderItem {
  value: number;
  valueStr: string;
  label: string;
}

export interface OrderDetailName {
  id: number;
  name: string;
}

export interface OrderDetailType {
  id: number;
  name: string;
  fee: number;
}

export const orderDetailNames: OrderDetailName[] = [
  {
    id: 0,
    name: '黄料',
  },
  {
    id: 1,
    name: '黄金',
  },
  {
    id: 2,
    name: '金条',
  },
  {
    id: 3,
    name: '铂金',
  },
  {
    id: 4,
    name: '钯金',
  },
  {
    id: 5,
    name: '银饰',
  },
  {
    id: 6,
    name: '足铂',
  },
  {
    id: 7,
    name: '足钯',
  },
  {
    id: 8,
    name: '足银',
  },
];

export const orderBobaDetailNames: OrderDetailName[] = [
  {
    id: 0,
    name: 'pt999',
  },
  {
    id: 2,
    name: 'pt990',
  },
  {
    id: 3,
    name: 'pt950',
  },
  {
    id: 4,
    name: 'pt900',
  },
  {
    id: 5,
    name: 'pd999',
  },
  {
    id: 6,
    name: 'pd990',
  },
  {
    id: 7,
    name: 'pd950',
  },
  {
    id: 8,
    name: 'pd900',
  },
  {
    id: 9,
    name: 'ag999',
  },
  {
    id: 10,
    name: 'ag990',
  },
  {
    id: 11,
    name: 'ag950',
  },
  {
    id: 12,
    name: 'ag900',
  },
];

export const orderDetailTypes: OrderDetailType[] = [
  {
    id: 0,
    name: '旧饰',
    fee: 0.3,
  },
  {
    id: 1,
    name: '万饰',
    fee: 0.1,
  },
  {
    id: 2,
    name: '黄块',
    fee: 0.3,
  },
  {
    id: 3,
    name: 'K饰',
    fee: 0.3,
  },
  {
    id: 4,
    name: 'K块',
    fee: 0.3,
  },
  {
    id: 5,
    name: '金条',
    fee: 0.1,
  },
  {
    id: 6,
    name: '铂金',
    fee: 0,
  },
  {
    id: 7,
    name: '钯金',
    fee: 0,
  },
  {
    id: 8,
    name: '银',
    fee: 0,
  },
];

export const orderLst: OrderItem[] = [
  {
    value: 0,
    valueStr: 'in',
    label: '入料开单',
  },
  {
    value: 1,
    valueStr: 'out',
    label: '出库开单',
  },
  {
    value: 2,
    valueStr: 'sale',
    label: '销货开单',
  },
  {
    value: 3,
    valueStr: 'price',
    label: '结价开单',
  },
  {
    value: 4,
    valueStr: '',
    label: '铂钯熔测',
  },
  {
    value: 5,
    valueStr: 'boba',
    label: '铂钯结价',
  },
];

export const useOrderStore = defineStore('app-order', () => {
  const curOrder = reactive<OrderItem>({
    value: 0,
    valueStr: 'in',
    label: '入料开单',
  });
  /**
   * 一般是dictTag使用
   */
  const dictMap = reactive(new Map<number, string>());

  function getOrderList(): OrderItem[] {
    return orderLst;
  }

  function getOrder(dictName: number): string {
    if (dictMap.keys.length === 0) {
      orderLst.forEach((item) => {
        dictMap.set(item.value, item.label);
      });
    }
    if (!dictName) return '';
    // 没有key 添加一个空数组
    if (!dictMap.has(dictName)) {
      dictMap.set(dictName, '');
    }
    // 这里拿到的就不可能为空了
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    return dictMap.get(dictName)!;
  }

  function resetCache() {
    dictMap.clear();
  }

  function setOrderInfo(dictName: number, dictValue: string) {
    if (
      (dictMap.has(dictName) && dictMap.get(dictName) === '') ||
      dictMap.get(dictName) === undefined
    ) {
      dictMap.set(dictName, dictValue);
    }
  }

  function $reset() {
    /**
     * doNothing
     */
  }

  return {
    $reset,
    dictMap,
    curOrder,
    resetCache,
    getOrderList,
    getOrder,
    setOrderInfo,
  };
});

// 通过类型获取类型的单价
export function getPriceByOrderDetailType(type: number) {
  const find = orderDetailTypes.find((item) => item.id === type);
  if (find === undefined) {
    return 0;
  }
  return find.fee;
}
